//
//  ClassSetWithCell.m
//  GIFShow
//
//  Created by lanou11air029 on 15/4/24.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "ClassSetWithCell.h"
#import "ClassLabelModel.h"
@interface ClassSetWithCell ()
@property (nonatomic, strong) UILabel * textTitleLabel;
@property (nonatomic, strong) UIView * underView;
@property (nonatomic) CGRect lastFrame;
@end
@implementation ClassSetWithCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubViews];
    }
    return self;
}
-(void)setupSubViews{
    CGRect screenBounds = [UIScreen mainScreen].bounds;
//    CGFloat orignY = screenBounds.size.height * 0.09;
//    CGFloat btnHeight = screenBounds.size.height * 0.25;
    self.textTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, screenBounds.size.height * 0.085)];
    self.lastFrame = CGRectMake(0, 0, 0, 0);

    self.textTitleLabel.text = @"Gif分类管理";
    self.textTitleLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:self.textTitleLabel];
    self.underView = [[UIView alloc]initWithFrame:CGRectMake(0, screenBounds.size.height*0.086, screenBounds.size.width, 480)];
    [self.contentView addSubview:self.underView];
    
        self.labelsArray = [NSMutableArray array];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString * path = [[NSBundle mainBundle]pathForResource:@"LabelList" ofType:@""];
            NSData * data = [NSData dataWithContentsOfFile:path];
            NSDictionary * sourceDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary * jsonDic = sourceDic[@"result"];
            for (NSDictionary * oneItem in jsonDic[@"hot"]) {
                ClassLabelModel * classModel = [[ClassLabelModel alloc]init];
                [classModel setValuesForKeysWithDictionary:oneItem];
                [self.labelsArray addObject:classModel.label_content];
            }
            for (NSDictionary * oneItem in jsonDic[@"list"]) {
                ClassLabelModel * classModel = [[ClassLabelModel alloc]init];
                [classModel setValuesForKeysWithDictionary:oneItem];
                [self.labelsArray addObject:classModel.label_content];
            }
            [self setupLabels];
        });
    
}
-(void)setupLabels{
    if (_labelsArray != nil) {
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        CGFloat orignY = screenBounds.size.height * 0.09;
        CGFloat btnWidth = screenBounds.size.width * 0.25;
        static NSInteger tag = 1000;
        tag ++;
        [UIView animateWithDuration:0.4 animations:^{
            int i = 0;
            for (NSString * title in self.labelsArray) {
                int row = 0;
                UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.lastFrame.origin.x + self.lastFrame.size.width + 10, self.lastFrame.origin.y + row * 40 , self.lastFrame.size.width, self.lastFrame.size.height)];
                titleLabel.text = title;
                titleLabel.tag = tag;
                [titleLabel sizeToFit];
                self.lastFrame = titleLabel.frame;
                if (titleLabel.frame.origin.x + titleLabel.frame.size.width > screenBounds.size.width) {
                    row++;
                    CGRect titleFrame = self.lastFrame;
                    titleFrame.origin.y = self.lastFrame.origin.y+row*40;
                    titleFrame.origin.x = 10;
                    titleFrame.size = CGSizeMake(0, 0);
                    titleLabel.frame = titleFrame;
                    [titleLabel sizeToFit];
                    self.lastFrame = titleLabel.frame;
                }
                [self.underView addSubview:titleLabel];
                titleLabel.layer.cornerRadius = 5;
                titleLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
                titleLabel.layer.borderWidth = 0.6;
                i++;
            }
        }];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGRect frame = self.contentView.frame;
    frame.size.width = [UIScreen mainScreen].bounds.size.width;
    if(selected){
        frame.size.height = height*0.64;
        [UIView animateWithDuration:0.4 animations:^{
            self.contentView.frame = frame;
            self.contentView.backgroundColor = [UIColor colorWithRed:0.95 green:1 blue:0.95 alpha:0.96];

        }];
      
        self.contentView.clipsToBounds = YES;
    }else{
        frame.size.height = height*0.085;
        [UIView animateWithDuration:0.4 animations:^{
            self.contentView.frame = frame;
//            self.contentView.backgroundColor = [UIColor whiteColor];
        }];
        self.contentView.clipsToBounds = YES;
    }
    for (UIView * view in self.underView.subviews) {
        view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.91];
    }
    // Configure the view for the selected state
}

@end
